package cn.school.util;

import java.sql.*;

public class DBUtil {
	private static String url = "jdbc:mysql://localhost:3306/java67?useUnicode=true&characterEncoding=UTF8&useSSL=false";
	private static String user = "zhaoyang";
	private static String pass = "zhaoyang";
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动没有找到");
			e.printStackTrace();
		}
	}

	private DBUtil() {

	}

	public static Connection getConnection() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, user, pass);
			return con;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void close(Connection con, Statement st) {
		close(st);
		close(con);
	}

	public static void close(ResultSet rs, Statement st, Connection con) {
		close(rs);
		close(st);
		close(con);
	}

	public static void close(ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(Statement st) {
		try {
			if (st != null)
				st.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(Connection con) {
		try {
			if (con != null)
				con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
